package com.example.demo.controller;

import com.example.demo.entity.request.User;
import com.example.demo.utils.JwtHelper;
import com.example.demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api("管理员用户API")
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
    @Value("${supadmin.account}")
    private String sup_admin_account;
    @Value("${supadmin.password}")
    private String sup_admin_password;

    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "account", name = "账号"),
            @ApiImplicitParam(value = "password", name = "密码")
    })
    @PostMapping("login")
    public Result login(@RequestBody @Validated User user) {
        String account = user.getAccount();
        String password = user.getPassword();
        String token;
        String access;

        if(account.equals(sup_admin_account) && password.equals(sup_admin_password)) {
            // 超级管理员
            access = "super_admin";
        } else {
            // 游客
            access = "tourist";
        }

        token = JwtHelper.sign(account, access);
        Map<String, String> data = new HashMap<>(2);
        data.put("token", token);
        data.put("access", access);
        return new Result(0, "success", data);
    }
}
